﻿
Namespace PL.MESSDK
    Public Class RoutingRevisionDetail_PL
        Inherits PLBase
        Implements IPLProcess

        Public Property ItemMasterID_FK As Guid?
        Public Property RoutingRevID_FK As Guid?

        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
        End Sub

        Public Sub New()
            MyBase.New()
        End Sub

        Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating
        End Sub

        Public Overrides Function Delete() As Boolean
            'Throw New NotImplementedException("Delete To Come...")
            Return False
        End Function

        Public Overrides Function Validate() As Boolean
            'Throw New NotImplementedException("Validate To Come...")
            Return True
        End Function

       
        Public Shared Sub RRDetail_Limite(aDataRow As DataRow)

            'TODO...
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("IMRRDetail_Revise")
                'Match les param[etre de la SP avec les colonne de la grilles
                For Each pr As System.Data.SqlClient.SqlParameter In sqlcmd.Parameters
                    If aDataRow.Table.Columns.Contains(pr.SourceColumn) Then
                        sqlcmd.Parameters(pr.ParameterName).Value = aDataRow(pr.SourceColumn)
                    End If
                Next
                sqlcmd.ExecuteNonQuery()
                'Fin TODO
            Catch sqlex As System.Data.SqlClient.SqlException

            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

        End Sub

        Public Shared Sub IMRevision_Copy(ByVal ItemMasterRevId As Guid, ByVal ItemMasterID_FK As Guid)

            'TODO...
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("IMRevision_CopyFrom")
                'Match les param[etre de la SP avec les colonne de la grilles

                sqlcmd.Parameters("@ItemMasterRevId").Value = ItemMasterRevId
                sqlcmd.Parameters("@ItemMasterID_FK").Value = ItemMasterID_FK

                sqlcmd.ExecuteNonQuery()
                'Fin TODO
            Catch sqlex As System.Data.SqlClient.SqlException

            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

        End Sub

        Public Shared Sub RRevision_Copy(ByVal routingRevID As Guid, ByVal itemMasterRevId As Guid)
            'TODO...
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("IMRRevision_CopyFrom")

                sqlcmd.Parameters("@RoutingRevID").Value = routingRevID
                sqlcmd.Parameters("@ItemMasterRevId").Value = itemMasterRevId

                sqlcmd.ExecuteNonQuery()
                'Fin TODO
            Catch sqlex As System.Data.SqlClient.SqlException

            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

        End Sub

        Public Overrides Sub ValidateRow(row As DataRow)
            row.ClearErrors()

            If row.Table.Columns.Contains("OperationId_FK") Then
                If IsDBNull(row("OperationId_FK")) Then
                    row.SetColumnError("OperationId_FK", "Mandatory")
                End If
            End If

            'If row.Table.Columns.Contains("WorkCenterId_FK") Then
            '    If IsDBNull(row("WorkCenterId_FK")) Then
            '        row.SetColumnError("WorkCenterId_FK", "Mandatory")
            '    End If
            'End If

            If row.Table.Columns.Contains("RoutingStep") Then
                If IsDBNull(row("RoutingStep")) Then
                    row.SetColumnError("RoutingStep", "Mandatory")
                End If
            End If

            'If row.Table.Columns.Contains("Scaling") Then
            '    If IsDBNull(row("Scaling")) Then
            '        row.SetColumnError("Scaling", "Mandatory")
            '    End If
            'End If


            If row.HasErrors Then
                row.RowError = "Please check your columns, a validation error occur"
            End If
        End Sub

        Public Function BatchRecords(dataRow() As System.Data.DataRow) As Boolean Implements IPLProcess.BatchRecords
            Return True
        End Function

        Public Function CurrentRecord(dataRow As System.Data.DataRow) As Boolean Implements IPLProcess.CurrentRecord
            Return True
        End Function

        Public Function NewRecord(dataRow As System.Data.DataRow) As Boolean Implements IPLProcess.NewRecord
            'Si on arrive d'un TreeList...
            If ItemMasterID_FK IsNot Nothing Then
                dataRow("ItemMasterID_FK") = ItemMasterID_FK
            End If

            If IsDBNull(dataRow("CompulsoryQC")) Then
                dataRow("CompulsoryQC") = 0
            End If

            If IsDBNull(dataRow("EnergyCost")) Then
                dataRow("EnergyCost") = 1
            End If

            If IsDBNull(dataRow("DirectLaborCost")) Then
                dataRow("DirectLaborCost") = 1
            End If

            If IsDBNull(dataRow("OvhCost")) Then
                dataRow("OvhCost") = 1
            End If


            If RoutingRevID_FK IsNot Nothing Then
                dataRow("RoutingRevID_FK") = RoutingRevID_FK
            End If

            'En phase out c'était bon pour VB - aleblanc 2012-04-30
            'dataRow("BegRev") = 0
            'dataRow("EndRev") = 999

            dataRow("RoutingRevDetailID") = Guid.NewGuid
            dataRow("RoutingId") = Guid.NewGuid

            Return True
        End Function

    End Class
End Namespace
